<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.MatchString"
              clearable
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      ref="tableSortRef"
      v-loading="listLoading"
      :border="border"
      :data="list"
      :height="height"
      :size="lineHeight"
      :stripe="stripe"
      @selection-change="setSelectRows"
    >
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :sortable="item.sortable"
        width="auto"
      >
        <template slot-scope="scope">
          <el-popover
            v-if="item.label === '适用产品'"
            placement="top-start"
            title="适用产品"
            :width="200"
            trigger="hover"
          >
            <template slot="reference">
              <el-button class="m-2">详情</el-button>
            </template>
            <template slot="default">
              {{ scope.row.ApplyProduct }}
            </template>
          </el-popover>
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="185"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
          <el-button type="text" @click="setyuzhi(row)">设置阈值</el-button>
        </template>
      </el-table-column>

      <template slot="empty">
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />

    <el-dialog
      :close-on-click-modal="false"
      :visible="dialogVisible"
      :title="title"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :model="form">
        <el-form-item label="毛坯类型" :label-width="formLabelWidth">
          <el-input
            v-model="form.MaterialType"
            :rows="2"
            type="text"
            :readonly="title == '编辑'"
          />
        </el-form-item>
        <el-form-item label="适用产品" :label-width="formLabelWidth">
          <el-table
            ref="multipleTableRef"
            :data="ProductInfoData"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55" />
            <el-table-column label="产品编号" width="120">
              <template slot-scope="scope">
                {{ scope.row.ProducationId }}
              </template>
            </el-table-column>
            <el-table-column label="产品名称" width="120">
              <template slot-scope="scope">
                {{ scope.row.ProducationName }}
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <!-- <el-form-item label="数量" :label-width="formLabelWidth">
          <el-input v-model="form.MaterialNum" :rows="2" type="text" />
        </el-form-item> -->
        <el-form-item label="长度" :label-width="formLabelWidth">
          <el-input
            v-model="form.Length"
            :rows="2"
            type="text"
            :readonly="title == '编辑'"
          />
        </el-form-item>
        <el-form-item label="宽度" :label-width="formLabelWidth">
          <el-input
            v-model="form.Width"
            :rows="2"
            type="text"
            :readonly="title == '编辑'"
          />
        </el-form-item>
        <el-form-item label="高度" :label-width="formLabelWidth">
          <el-input
            v-model="form.Height"
            :rows="2"
            type="text"
            :readonly="title == '编辑'"
          />
        </el-form-item>
        <el-form-item label="阈值" :label-width="formLabelWidth">
          <el-input
            v-model="form.Threshold"
            :rows="2"
            type="text"
            :readonly="title == '编辑'"
          />
        </el-form-item>
      </el-form>
      <template slot="footer">
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submit">保存</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog
      :close-on-click-modal="false"
      :visible="showDialog"
      width="500px"
      :before-close="handleClose2"
    >
      <div>
        <el-input v-model="yuzhi" placeholder="请输入阈值"></el-input>
      </div>
      <template slot="footer">
        <span class="dialog-footer">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button @click="yzconfirm" type="primary">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import {
    MaterialTypeManager,
    MaterialType,
    MaterialTypeDelete,
    GetProduction,
    SetThreshold,
    MaterialTypeUpdate,
  } from '@/api/material'

  export default {
    data() {
      return {
        yuzhi: '',
        showDialog: false,
        curdetail: {},
        TrayInfoData: [],
        form: {
          Id: '',
          MaterialType: '',
          Content: '',
          ApplyProduct: '',
          Creator: '',
          CreateTime: new Date(),
          MaterialNum: '0',
          Length: '',
          Width: '',
          Height: '',
        },
        radio: 3,
        formLabelWidth: '100px',
        dialogVisible: false,
        tableSortRef: null,
        editRef: null,
        border: true,
        height: this.$baseTableHeight(1),
        stripe: false,
        lineHeight: 'small',
        isFullscreen: false,
        checkList: [
          '毛坯类型',
          '适用产品',
          '创建时间',
          '创建者',
          '长度',
          '宽度',
          '高度',
          '阈值',
        ],
        columns: [
          {
            label: '毛坯类型',
            prop: 'MaterialType',
          },
          {
            label: '适用产品',
            prop: 'ApplyProduct',
          },
          {
            label: '创建时间',
            prop: 'CreateTime',
          },
          {
            label: '创建者',
            prop: 'Creator',
          },
          {
            label: '数量',
            prop: 'MaterialNum',
          },
          {
            label: '长度',
            prop: 'Length',
          },
          {
            label: '宽度',
            prop: 'Width',
          },
          {
            label: '高度',
            prop: 'Height',
          },
          {
            label: '阈值',
            prop: 'Threshold',
          },
        ],
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 20,
          MatchString: '',
        },
        title: '添加',
        ProductInfoData: [],
      }
    },
    computed: {
      ...mapGetters({
        username: 'user/username',
      }),
      finallyColumns() {
        return this.columns.filter((item) =>
          this.checkList.includes(item.label)
        )
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      yzconfirm() {
        this.showDialog = false
        SetThreshold({
          MaterialType: this.curdetail.MaterialType,
          Threshold: this.yuzhi,
        }).then((res) => {})
      },
      handleSelectionChange(e) {
        this.form.ApplyProduct = e.map((item) => {
          return item.ProducationId
        })
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      submit() {
        this.form.Creator = this.username
        this.form.CreateTime = this.days()
        if (this.title == '添加') {
          MaterialType(this.form).then((res) => {
            console.log(res)
            this.$baseMessage('成功', 'success', 'vab-hey-message-success')
            this.dialogVisible = false
            fetchData()
          })
        } else {
          MaterialTypeUpdate(this.form).then((res) => {
            console.log(res)
            this.$baseMessage('成功', 'success', 'vab-hey-message-success')
            this.dialogVisible = false
            fetchData()
          })
        }
      },
      handleEdit(row) {
        this.productData()
        if (row.MaterialType) {
          this.title = '编辑'
          this.form = row
        } else {
          this.title = '添加'
        }

        this.dialogVisible = true
      },
      productData() {
        GetProduction().then((res) => {
          console.log('xxxxxxx', res.ProductInfo)
          this.ProductInfoData = res.ProductInfo
        })
      },
      setyuzhi(row) {
        this.showDialog = true
        this.curdetail = row
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await MaterialTypeDelete({
            MaterialType: row.MaterialType,
          })
          // $baseMessage(msg, 'success', 'vab-hey-message-success')
          await this.fetchData()
        })
        return false
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleClose() {
        this.dialogVisible = false
      },
      handleClose2() {
        this.showDialog = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const params = {
          Index: this.getPage(this.queryForm.pageNo, this.queryForm.pageSize),
          Count: this.queryForm.pageSize,
          MatchString: this.queryForm.MatchString,
        }
        MaterialTypeManager(params).then((res) => {
          console.log('MaterInfo', res)
          this.list = res.MaterialTypeList
          this.list.forEach((item) => {
            item.ApplyProduct = item.ApplyProduct.join(',')
            console.log('tiem.ApplyProduct')
          })
          this.total = res.TotalCount
          this.listLoading = false
        })
      },
    },
  }
</script>
